<template>
  <div class="user-content">
    <div class="header">
      <h3 class="title">用户列表</h3>
      <el-button type="primary" @click="handleNewUserClick">新建用户</el-button>
    </div>
    <div class="table">
      <el-table :data="usersList" border style="width: 100%">
        <el-table-column align="center" type="selection" width="60px" />
        <el-table-column
          align="center"
          type="index"
          label="序号"
          width="80px"
        />
        <el-table-column
          align="center"
          prop="name"
          label="用户名"
          width="180px"
        />
        <el-table-column
          align="center"
          prop="realname"
          label="真实姓名"
          width="150px"
        />
        <el-table-column
          align="center"
          prop="cellphone"
          label="电话号码"
          width="200px"
        />
        <el-table-column
          align="center"
          prop="enable"
          label="状态"
          width="100px"
        >
          <!--作用域插槽  -->
          <template #default="scope">
            <el-button :type="scope.row.enable ? 'success' : 'danger'" plain>
              {{ scope.row.enable ? '启用' : '禁用' }}
            </el-button>
          </template>
        </el-table-column>
        <el-table-column
          align="center"
          prop="createAt"
          label="创建时间"
          width="300px"
        >
          <template #default="scope">
            {{ formatUTC(scope.row.createAt) }}
          </template>
        </el-table-column>
        <el-table-column
          align="center"
          prop="updateAt"
          label="更新时间"
          width="300px"
        >
          <template #default="scope">
            {{ formatUTC(scope.row.updateAt) }}
          </template>
        </el-table-column>
        <el-table-column align="center" label="操作">
          <template #default="scope">
            <el-button
              type="primary"
              icon="Edit"
              text
              @click="handleEditBtnClick(scope.row)"
              >编辑</el-button
            >
            <el-button
              type="danger"
              icon="Delete"
              text
              @click="handleDelete(scope.row.id)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
    </div>

    <!-- 分页 -->
    <div class="pagination">
      <el-pagination
        v-model:current-page="currentPage"
        v-model:page-size="pageSize"
        :page-sizes="[10, 20, 30]"
        :small="small"
        layout="total, sizes, prev, pager, next, jumper"
        :total="usersTotalCount"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </div>
  </div>
</template>

<script setup lang="ts">
import { useSystemStore } from '@/store/main/system/system'
import { storeToRefs } from 'pinia'
import { formatUTC } from '@/utils/format'
import { ref } from 'vue'
const emit = defineEmits(['newClick', 'editClick'])
// 请求用户数据
const systemStore = useSystemStore()
const currentPage = ref(1)
const pageSize = ref(10)

fetchUserListData()
const { usersList, usersTotalCount } = storeToRefs(systemStore)

// 分页
const small = ref(false)
function handleSizeChange() {
  fetchUserListData()
}
function handleCurrentChange() {
  fetchUserListData()
}

// 删除数据
function handleDelete(userId: number) {
  systemStore.postUsersDeleteAction(userId)
  // systemStore.deleteUserAction(row.id)
}

// 编辑用户
function handleEditBtnClick(userInfo: any) {
  console.log(userInfo)

  emit('editClick', userInfo)
}

// 新建用户
function handleNewUserClick() {
  emit('newClick')
}

// 定义函数，用于发送网络请求
function fetchUserListData(formData: any = {}) {
  // 1.获取size和offset参数
  const offset = (currentPage.value - 1) * pageSize.value
  const size = pageSize.value
  const pageInfo = { size, offset }

  // 合并formData
  const queryInfo = { ...pageInfo, ...formData }

  // 2.发送请求
  systemStore.postUsersListAction(queryInfo)
}

defineExpose({ fetchUserListData, handleEditBtnClick })
</script>

<style lang="less" scoped>
.user-content {
  background: #fff;
  margin-top: 20px;
  padding: 20px;
  .header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 10px;
    .title {
      font-size: 22px;
    }
  }
  .pagination {
    display: flex;
    justify-content: flex-end;
    margin-top: 10px;
  }
}
// .table {
//   :deep(.el-table__cell) {
//     padding: 15px 0;
//   }
// }
</style>
